package com.niuxiaofa.newlaw.Base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.View.LoadingDialog;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.Disposable;

/**
 * Created by gxjkz on 2016/6/7.
 */
public abstract class BaseFragment extends Fragment {

    public boolean isVisible;
    public static final String FRAGMENT_INDEX = "fragment_index";
    public int index = 0;
    protected Disposable disposable;
    private Unbinder mUnBinder;
    private LoadingDialog loadingDialog;

    public View view;

    public RefreshLayout refreshLayout;

    public RefreshInterface refreshinterface;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            index = bundle.getInt(FRAGMENT_INDEX);
        }
        this.loadingDialog = new LoadingDialog(getContext());
        this.loadingDialog.setCanceledOnTouchOutside(false);




    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        mUnBinder = ButterKnife.bind(this, view);
        initData();

        try {

        refreshLayout = view.findViewById(R.id.refreshLayout);
        if (refreshLayout != null) {
            refreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    refreshinterface.refresh.refresh();
                }
            });
            refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(RefreshLayout refreshlayout) {
                    refreshinterface.refresh.loadmore();
                }
            });

        }
        }catch (Exception e){

        }

        return view;
    }


    protected abstract int getLayoutId();

    public abstract void initData();

    public  interface Refresh{

        public void refresh();
    }





    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
        undisposable();
    }


    protected void undisposable() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    public void Toast(String content) {
        Toast.makeText(UIUtils.getContext(), content, Toast.LENGTH_LONG).show();
    }

    public void dismissDialog() {
        if (this.loadingDialog.isShowing()) {
            this.loadingDialog.dismiss();
        }
    }

    public void showLoadingDialog() {
        showLoadingDialog(true);
    }

    public void showLoadingDialog(String str, boolean z) {
        if (!this.loadingDialog.isShowing()) {
            this.loadingDialog.setCancelable(z);
            this.loadingDialog.show();
            this.loadingDialog.setText(str);
        }
    }

    public void showLoadingDialog(boolean z) {
        showLoadingDialog("请稍等...", z);
    }


//    public void onResume() {
//        super.onResume();
//        MobclickAgent.onPageStart(getClass().getName()); //统计页面，"MainScreen"为页面名称，可自定义
//    }
//
//    public void onPause() {
//        super.onPause();
//        MobclickAgent.onPageEnd(getClass().getName());
//    }


}